<?php
include_once dirname(dirname(dirname(__FILE__))) . '/include/common.php';
global $core, $category_id;
$html_category = '';
$category_id = 0;
$core = new core();
$html_category = show_category(0);

function getCategoryByParentId($parent_id) {
    global $core;
    if ($core->db) {
        $query = "SELECT * FROM zen_crawl_category WHERE parent_id =$parent_id";
        $category = $core->db->get_results($query);

        return $category;
    } else {
        return false;
    }
}

function getTotalChildCategoryById($category_id) {
    global $core;
    if ($core->db) {
        $query = "SELECT count(id) as total FROM zen_crawl_category WHERE parent_id =$category_id";
        $category = $core->db->get_row($query);

        return $category->total;
    } else {
        return false;
    }
}

function show_category($parent_id = 0) {
    global $core, $category_id;
    $html = '';
    $category = getCategoryByParentId($parent_id);

    if ($category) {
        foreach ($category as $cate) {
            $total_child_category = getTotalChildCategoryById($cate->id);

            if ($total_child_category > 0) {
                $html.='<optgroup label="' . $cate->nav_name . '">';
                $html.=show_category($cate->id);
                $html.='</optgroup>';
            } else {
                if ($category_id == $cate->id) {
                    $html.=' <option selected value="' . $cate->id . '">' . $cate->nav_name . '</option>';
                } else {
                    $html.=' <option value="' . $cate->id . '">' . $cate->nav_name . '</option>';
                }
            }
        }
    }
    return $html;
}
?>
<div class="controls">
    <select  id="product_category" >
        <option value=""></option>
<?php echo $html_category; ?>

    </select>
</div>